local skel = fk.CreateSkill {
  name = "lb_zero__shuangyi",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable {
  ["lb_zero__shuangyi"] = "霜刈",
  [":lb_zero__shuangyi"] = "每回合限一次，出牌阶段，你可令你本回合的手牌均视为造成冰冻伤害的随机单目标伤害牌。结束阶段，若你本回合造成伤害数与因弃置进入弃牌堆的牌数相等，你依次执行所有主要阶段，否则你翻面。",

  ["#lb_zero__shuangyi"] = "霜刈：你可令你本回合的手牌均视为造成冰冻伤害的随机单目标伤害牌",

  ["@@lb_zero__shuangyi-inhand-turn"] = "霜刈",
  ["@[damage_card]lb_zero__shuangyi-turn"] = "霜刈",

  ["$lb_zero__shuangyi1"] = "别乱动。",
  ["$lb_zero__shuangyi2"] = "席卷一空。",
  ["$lb_zero__shuangyi3"] = "真是麻烦。",
  ["$lb_zero__shuangyi4"] = "鲨旋风！",
  ["$lb_zero__shuangyi5"] = "冲走吧！",
}

-- 传入值：整数数组（damage, card）
Fk:addQmlMark {
  name = "damage_card",
  qml_path = "",
  how_to_show = function(_, value)
    if type(value) ~= "table" or #value ~= 2 then return " " end
    return "<font color='red'>" .. "伤" .. "</font>" .. value[1] ..
        " " .. "<font color='#0598BC'>" .. "牌" .. "</font>" .. value[2]
  end,
}

local ChangHandCardMark = function(player)
  local room = player.room
  local cards = player:getCardIds("h")
  if #cards == 0 then return end
  local names = table.filter(Fk:getAllCardNames("bt"), function(name)
    local card = Fk:cloneCard(name)
    return card.is_damage_card and not card.multiple_targets -- 应该没有不指定目标的伤害牌吧
  end)
  if #names == 0 then return end
  for _, id in ipairs(cards) do
    local card = Fk:getCardById(id)
    if card:getMark("@@lb_zero__shuangyi-inhand-turn") == 0 then
      room:setCardMark(card, "@@lb_zero__shuangyi-inhand-turn", table.random(names))
    end
  end
end
-- 标记里用不了记录器，麻烦一点吧
local ChangShuangYiMark = function(player)
  local room = player.room
  local logic = room.logic
  local damage_num, discard_num = 0, 0
  logic:getEventsOfScope(GameEvent.Damage, 9999, function(e)
    local damage = e.data
    if damage.from == player and not damage.prevented then
      damage_num = damage_num + damage.damage
    end
  end, Player.HistoryTurn)
  for _, id in ipairs(DIY.getCenterCards(room)) do
    logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
      local move = e.data
      for i = #move, 1, -1 do
        local moveInfo = move[i].moveInfo
        for j = #moveInfo, 1, -1 do
          if moveInfo[j].cardId == id then
            if move[i].moveReason == fk.ReasonDiscard then   -- 只找最后一次移动
              discard_num = discard_num + 1
            end
            return true
          end
        end
      end
    end, nil, Player.HistoryTurn)
  end
  if damage_num > 0 or discard_num > 0 then
    room:setPlayerMark(player, "@[damage_card]lb_zero__shuangyi-turn", { damage_num, discard_num })
  else
    room:setPlayerMark(player, "@[damage_card]lb_zero__shuangyi-turn", 0)
  end
end

skel:addEffect("active", {
  audio_index = {1, 2, 3},
  anim_type = "offensive",
  prompt = "#lb_zero__shuangyi",
  target_num = 0,
  card_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name) == 0
  end,
  on_use = function(self, room, skillUseEvent)
    local player = skillUseEvent.from
    room:setPlayerMark(player, "lb_zero__shuangyi-turn", 1)
    ChangHandCardMark(player)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:getMark("lb_zero__shuangyi-turn") > 0 then
      for _, move in ipairs(data) do
        if (move.from == player and table.find(move.moveInfo, function(info)
              return info.fromArea == Card.PlayerHand
            end)) or (move.to == player and move.toArea == Card.PlayerHand) then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    ChangHandCardMark(player)
  end,
})

skel:addEffect(fk.PreCardUse, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card:getMark("@@lb_zero__shuangyi-inhand-turn") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.ChangeDamageType = fk.IceDamage
  end,
})

skel:addEffect("filter", {
  mute = true,
  card_filter = function(self, card, player)
    return card:getMark("@@lb_zero__shuangyi-inhand-turn") ~= 0
  end,
  view_as = function(self, player, card)
    local damage_card = Fk:cloneCard(card:getMark("@@lb_zero__shuangyi-inhand-turn"), card.suit, card.number)
    damage_card.skillName = skel.name
    return damage_card
  end,
})


skel:addEffect(fk.EventPhaseEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local damage_num, discard_num = 0, 0
    local mark = player:getTableMark("@[damage_card]lb_zero__shuangyi-turn")
    if #mark == 2 then
      damage_num, discard_num = mark[1], mark[2]
    end
    if damage_num == discard_num then
      room:notifySkillInvoked(player, skel.name, "special")
      player:broadcastSkillInvoke(skel.name, math.random(4, 5))
      local list = { "phase_judge", "phase_draw", "phase_play", "phase_discard" }
      for i = #list, 1, -1 do
        player:gainAnExtraPhase(Util.PhaseStrMapper(list[i]), skel.name)
      end
    else
      room:notifySkillInvoked(player, skel.name, "negative")
      player:broadcastSkillInvoke(skel.name, math.random(1, 3))
      player:turnOver()
    end
  end,
})
-- 宝宝标记
skel:addEffect(fk.Damage, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true) and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    ChangShuangYiMark(player)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name, true) then
      for _, move in ipairs(data) do
        if table.find(move.moveInfo, function(info)
              return info.fromArea == Card.DiscardPile
            end) or move.toArea == Card.DiscardPile then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    ChangShuangYiMark(player)
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
  ChangShuangYiMark(player)
end)

skel:addLoseEffect(function(self, player, is_death)
  for _, id in ipairs(player:getCardIds("h")) do
    player.room:setCardMark(Fk:getCardById(id), "@@lb_zero__shuangyi-inhand-turn", 0)
  end
  player.room:setPlayerMark(player, "lb_zero__shuangyi-turn", 0)
  player.room:setPlayerMark(player, "@[damage_card]lb_zero__shuangyi-turn", 0)
end)

return skel
